using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Spike.ContractBuilder;
using Spike.TestHarness.Properties;

namespace Spike.TestHarness
{
    public partial class AddHostDialog : Form
    {
        public AddHostDialog()
        {
            InitializeComponent();

            LoadContracts();
        }

        private void LoadContracts()
        {
            cboContracts.DataSource = ContractGenerator.Instance.GetServiceContracts();
        }

        private void txtName_Validating(object sender, CancelEventArgs e)
        {
            if (txtName.Text.Length < 1 || ContractGenerator.Instance.Hosts.ContainsKey(txtName.Text))
                errorProvider1.SetError(txtName, "Please enter a unique Service Name.");
        }

        private void cboContracts_Validating(object sender, CancelEventArgs e)
        {

        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (!this.Validate())
                return;

            try
            {
                ContractGenerator.Instance.AddHost(txtName.Text, cboContracts.Text, txtAddress.Text);
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
            catch (Exception ex)
            {
                errorProvider1.SetError(cboContracts, "Failed to create host configuration file: " + ex.Message);
            }
        }

        private void txtAddress_Validating(object sender, CancelEventArgs e)
        {
            if (txtAddress.Text.Length < 1)
                errorProvider1.SetError(txtAddress, "Please enter a valid http address.");
        }
    }
}